package org.droid.campusorder.adapter;

import java.io.File;
import java.util.List;

import org.droid.R;
import org.droid.campusorder.bean.DishesBean;
import org.droid.campusorder.common.Debug;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.GridLayoutAnimationController.AnimationParameters;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
	private Context context; // 运行上下文
	private List<DishesBean> listItems; // 菜品信息集合
	private LayoutInflater listContainer;
	private String[] dishPicPaths = null;
	private ListItemView listItemView = null;
	private Animation totalPriceStartAnimation;  // 动画对象
	private Animation totalPriceEndAnimation;  // 动画对象
	private boolean flag = false;
	private Debug debug;
	
	public String[] getDishPicPaths() {
		return dishPicPaths;
	}
	public void setDishPicPaths(String[] dishPicPaths) {
		this.dishPicPaths = dishPicPaths;
	}

	public void setListItems(List<DishesBean> listItems) {
		this.listItems = listItems;
	}

	public ListViewAdapter(Context context) {
		debug = new Debug("ListViewAdapter");
		debug.debug("ListViewAdapter construct");
		this.context = context;
		listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
		totalPriceStartAnimation = AnimationUtils.loadAnimation(context,R.anim.total_price_scale_start);
		totalPriceEndAnimation = AnimationUtils.loadAnimation(context,R.anim.total_price_scale_end);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItems == null ? 0 : listItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean changeView(int position) {
		//debug.debug("now flag = "+flag);
		/*if (flag){
			flag = false;
			debug.debug("change flag = false");
		}else{
			flag = true;
			debug.debug("change flag = true");
		}*/
		if (listItems.get(position).isFlag()){
			listItems.get(position).setFlag(false);
		}else {
			listItems.get(position).setFlag(true);
		}
		notifyDataSetChanged();
		return listItems.get(position).isFlag();
	}

	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义view ，ListView单击事件未写
		debug.debug("getView()");
		final int selectID = position;
//		ListItemView listItemView = null;
		if (convertView == null) {
			listItemView = new ListItemView();
			// 获取list_item布局文件的视图
			convertView = listContainer.inflate(R.layout.dishes_item, null);
			// 获取控件对象
			listItemView.picture = (ImageView) convertView
					.findViewById(R.id.dishes_imv);
			listItemView.dishesName = (TextView) convertView
					.findViewById(R.id.dish_name_txt);
			listItemView.price = (TextView) convertView
					.findViewById(R.id.price_txt);
			listItemView.orderedImv = (ImageView) convertView
					.findViewById(R.id.ordered_imv);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}
		// 设置文字

		listItemView.dishesName.setText(listItems.get(position).getName());
		listItemView.price.setText(listItems.get(position).getPrice());
		File file = new File(dishPicPaths[position].trim());
		if (!file.exists()) {
			listItemView.picture.setImageResource(R.drawable.icon);
		} else {
			Bitmap btm = BitmapFactory
					.decodeFile(dishPicPaths[position].trim());
			listItemView.picture.setImageBitmap(btm);
			btm = null;
		}
		listItemView.picture.setBackgroundResource(R.drawable.shoppic_bg);
		
		debug.debug("getView position →"+position);
		debug.debug("FLAG →"+listItems.get(position).isFlag());
		if (listItems.get(position).isFlag()){
			debug.debug("设置ImageView可见");
			listItemView.orderedImv.setVisibility(View.VISIBLE);
			//if (listItemView.orderedImv.getVisibility() == View.GONE){
				//listItemView.orderedImv.setVisibility(View.VISIBLE);
			/*if (!totalPriceStartAnimation.hasStarted()||totalPriceStartAnimation.hasEnded()){
				listItemView.orderedImv.startAnimation(totalPriceStartAnimation);
				listItemView.orderedImv.setImageResource(R.drawable.dish_order_nm);
			}*/
				//listItems.get(position).setFlag(false);
			//}else if (listItemView.orderedImv.getVisibility() == View.VISIBLE){
			//	listItemView.orderedImv.setVisibility(View.GONE);
				/*listItemView.orderedImv.startAnimation(totalPriceEndAnimation);
				listItems.get(position).setFlag(false);*/
		//	}
			/*listItemView.orderedImv.setVisibility(View.VISIBLE);
			listItemView.orderedImv.startAnimation(totalPriceStartAnimation);*/
		//}else {
			//listItemView.orderedImv.setVisibility(View.GONE);
			//listItemView.orderedImv.startAnimation(totalPriceEndAnimation);
		} else{
			debug.debug("该结束了");
			listItemView.orderedImv.setVisibility(View.GONE);
			//listItemView.orderedImv.setImageResource(0);
			//listItemView.orderedImv.setVisibility(View.GONE);
			//listItemView.orderedImv.startAnimation(totalPriceEndAnimation);
			//listItemView.orderedImv.setVisibility(View.GONE);
		}
		
		return convertView;
	}

	public final class ListItemView { // 自定义控件集合
		public ImageView picture;
		public TextView dishesName;
		public TextView price;
		public ImageView orderedImv;
	}
}
